import { observable, action } from 'mobx';
import { Bubble } from 'weaver-mobile';

import Api, { getUserGroupsList4Mobile } from '../../apis/contacts';
import TableListStore from '../setting/tableListStore';

const initState = {
  tableListParams: {
    url: getUserGroupsList4Mobile,
    params: { },
  },
};

export default class GroupSettingStore {
  @observable tableListStore = new TableListStore(initState.tableListParams);

  @action
  handleOperation = (params, cb) => {
    Api.contactOperation({ ...params }).then(res => {
      const { status, api_status, api_msg } = res;
      const then = () => {
        this.tableListStore.refreshTableList();
        if (cb) cb(res);
      };
      if (api_status !== undefined) {
        if (api_status) {
          then();
        } else {
          Bubble.error(api_msg, 1, { top: '50%' });
        }
      } else if (status === '1') {
        then();
      }
    });
  }

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  clear = () => this.setState(initState)
}
